package com.imooc.o2o.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imooc.o2o.dao.PersonInfoDao;
import com.imooc.o2o.dto.PersonInfoExecution;
import com.imooc.o2o.entity.PersonInfo;
import com.imooc.o2o.enums.PersonInfoStateEnum;
import com.imooc.o2o.exceptions.PersonInfoOperationException;
import com.imooc.o2o.service.PersonInfoService;

@Service
public class PersonInfoServiceImpl implements PersonInfoService{
	@Autowired
	private PersonInfoDao personInfoDao;
	
	@Override
	public PersonInfoExecution addPersonInfo(PersonInfo personInfo) throws PersonInfoOperationException {
		if(personInfo == null) {
			//用户信息为空
			return new PersonInfoExecution(PersonInfoStateEnum.NULL_PERSON);
		}
		try {
			//初始化信息
			personInfo.setCreateTime(new Date());
			personInfo.setEnableStatus("1");//该商店可以用
			personInfo.setUserType(2);//现在只设置只有卖家才需要注册,顾客只浏览就行了
			personInfo.setLastEditTime(new Date());
			int effectedNum = personInfoDao.insertPersonInfo(personInfo);
			if(effectedNum <= 0) {
				throw new PersonInfoOperationException("用户信息添加失败");
			}
		} catch (Exception e) {
			throw new PersonInfoOperationException("addPersonInfo error : " + e.getMessage());
		}	
		//返回
		return new PersonInfoExecution(PersonInfoStateEnum.SUCCESS, personInfo);
	}

	@Override
	public PersonInfo getPersonInfoById(Long userId) {
		//根据id获取用户信息
		return personInfoDao.queryPersonInfoById(userId);
	}
}
